package com.example.tellwin.home.presenter;

import com.apkfuns.logutils.LogUtils;
import com.example.tellwin.MainApplication;
import com.example.tellwin.R;
import com.example.tellwin.api.TwjfApi;
import com.example.tellwin.base.RxPresenter;
import com.example.tellwin.base.SimpleCallback;
import com.example.tellwin.home.bean.ActivitiesBean;
import com.example.tellwin.home.bean.ActivityContentBean;
import com.example.tellwin.home.bean.ColumnBean;
import com.example.tellwin.home.bean.ContentBean;
import com.example.tellwin.home.bean.QuestionBean;
import com.example.tellwin.home.bean.QuestionContentBean;
import com.example.tellwin.home.bean.TeactherTeamBean;
import com.example.tellwin.home.contract.NormalHomeContract;
import com.example.tellwin.utils.HttpUtils;
import com.example.tellwin.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import okhttp3.RequestBody;

/**
 * @author : LinZePeng
 * @date : 2020/11/25
 * @Describe ：
 **/
public class NormalHomePresenter extends RxPresenter<NormalHomeContract.View> implements NormalHomeContract.Presenter<NormalHomeContract.View> {

    private TwjfApi mApi;

    private final int pageSize = 10;

    @Inject
    public NormalHomePresenter(TwjfApi mApi) {
        this.mApi = mApi;
    }

    @Override
    public void homeBanner() {
        mApi.homeBanner(new SimpleCallback<ContentBean>(mView) {
            @Override
            public void onSuccess(ContentBean contentBean) {
                if (contentBean.getBannerList() != null) {
                    mView.homeBannerResult(contentBean.getBannerList());
                } else {
                    LogUtils.d("数据为空");
                }
            }

            @Override
            public void onFail(String msg) {
                mView.onFail();
            }
        });
    }

    @Override
    public void homeColumn() {
        mApi.homeColumn(new SimpleCallback<ContentBean>(mView) {
            @Override
            public void onSuccess(ContentBean contentBean) {
                if (contentBean.getColumnBeanList() != null) {
                    mView.homeColumnResult(contentBean.getColumnBeanList());
                } else {
                    LogUtils.d("数据为空");
                }
            }

            @Override
            public void onFail(String msg) {
                mView.onFail();
            }
        });
    }

    @Override
    public void indexQuestionList(int position, ColumnBean columnBean) {
        //RequestBody requestBody = HttpUtils.getRequestBody("page" , page+"" , "pageSize" ,pageSize+"" , "columnId" , columId);
        QuestionContentBean questionContentBean = columnBean.getContentBean();
        int page = 1;
        if (questionContentBean != null) {
            page = questionContentBean.getPageNum() + 1;
            if (page > questionContentBean.getPages()) {
                ToastUtil.show(MainApplication.getCurActivity(), R.string.none_data);
                return;
            }
        }
        LogUtils.d(columnBean.getColumnName() + "-" + page);
        mApi.indexQuestionList(page + "", pageSize + "", columnBean.getColumnId(), new SimpleCallback<QuestionContentBean>(mView) {
            @Override
            public void onSuccess(QuestionContentBean questionContentBean) {
                List<QuestionBean> questionBeans = new ArrayList<>();
                if (columnBean.getContentBean() != null && columnBean.getContentBean().getQuestionList() != null) {
                    questionBeans = columnBean.getContentBean().getQuestionList();
                }
                if (questionContentBean.getQuestionList() != null) {
                    questionContentBean.getQuestionList().addAll(0, questionBeans);
                } else {
                    questionContentBean.setQuestionList(questionBeans);
                }
                columnBean.setContentBean(questionContentBean);
                mView.indexQuestionListResult(position, columnBean);
            }

            @Override
            public void onFail(String msg) {
                mView.onFail();
            }
        });
    }

    @Override
    public void getTeacherTeam() {
        mApi.getTeacherTeam(new SimpleCallback<ContentBean>(mView) {
            @Override
            public void onSuccess(ContentBean contentBean) {
                List<TeactherTeamBean> teactherTeamBeans = contentBean.getFacultyTeamList();
                if (teactherTeamBeans == null) {
                    teactherTeamBeans = new ArrayList<>();
                }
                mView.teacherTeamResult(teactherTeamBeans);
            }

            @Override
            public void onFail(String msg) {
                mView.onFail();
            }
        });
    }

    @Override
    public void getActiviteList() {
        mApi.getActivityList("1", "5", new SimpleCallback<ActivityContentBean>(mView) {
            @Override
            public void onSuccess(ActivityContentBean contentBean) {
                List<ActivitiesBean> activitiesBeans = contentBean.getActivityList();
                if (activitiesBeans == null) {
                    activitiesBeans = new ArrayList<>();
                }
                mView.activiteListResult(activitiesBeans);
            }

            @Override
            public void onFail(String msg) {
                mView.onFail();
            }
        });
    }

    @Override
    public void likeQuestion(String questionId,int position) {
        RequestBody requestBody = HttpUtils.getRequestBody("followType", "0" + "", "relationId", questionId, "relationType", "0");
        mApi.follow(requestBody, new SimpleCallback<Object>(mView) {
            @Override
            public void onSuccess(Object o) {
                mView.likeQuestionResult(position);
            }

            @Override
            public void onFail(String msg) {
                mView.onFail();
            }
        });
    }
}
